package org.zf.database.domain;

import cn.hutool.core.util.StrUtil;
import lombok.*;
import org.zf.database.consts.DBType;

import java.io.Serializable;

/**
 * 数据库连接信息
 */
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class DBInfo implements Serializable
{
    private DBType dbType;
    private String owner;
    private String jdbcUrl;
    private ConnInfo connInfo;

    public DBInfo(DBType dbType)
    {
        this.dbType = dbType;
    }

    public DBInfo(DBType dbType, ConnInfo connInfo)
    {
        this.dbType = dbType;
        setConnInfo(connInfo);
    }


    public void setJdbcUrl(String jdbcUrl)
    {
        jdbcUrl = jdbcUrl.trim();
        this.jdbcUrl = jdbcUrl.endsWith("?") ? jdbcUrl.substring(0, jdbcUrl.length() - 1) : jdbcUrl;
    }

    public void setConnInfo(ConnInfo connInfo)
    {
        this.connInfo = connInfo;
        if (dbType == DBType.ORACLE)
            this.owner = StrUtil.isEmpty(connInfo.getUser()) ? null : connInfo.getUser().toUpperCase();
        else
            this.owner = connInfo.getDbName();
    }
}
